package com.gjs.freechat.web.controller;

import java.awt.image.BufferedImage;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gjs.freechat.server.business.ChatSessionBusiness;
import com.gjs.freechat.server.component.ChatSessionManage;
import com.gjs.freechat.server.component.bean.Conversation;
import com.gjs.freechat.server.component.bean.Message;
import com.gjs.freechat.web.business.WebChatSessionManage;

@RestController
@RequestMapping("/action/chatSessionManage")
public class ChatSessionController extends AbstractBaseController {
	
	@Autowired
	WebChatSessionManage webChatSessionManage;
	
	@Autowired
	ChatSessionManage chatSessionManage;
	
	@Autowired
	private ChatSessionBusiness chatSessionBusiness;
	
	@RequestMapping("/getAllConvos")
	public List<Conversation> getAllConvos() {
		return webChatSessionManage.getAllConvos(getRequest());
	}
	
	@RequestMapping("/clearUnreadMessages")
	public void clearUnreadMessages(@RequestParam("sessionId") String sessionId, @RequestParam("userId") String userId) {
		chatSessionManage.clearUnreadMessages(sessionId, userId);
	}
	
	@RequestMapping("/addNewConvo")
	public void addNewConvo(@RequestBody Conversation convo) {
		chatSessionBusiness.addNewConvo(convo);
	}
	
	@RequestMapping("/addUnreadMessage")
	public void addUnreadMessage(@RequestParam("sessionId") String sessionId, @RequestParam("userId") String userId) {
		chatSessionManage.addUnreadMessage(sessionId, userId);
	}
	
	@RequestMapping("/startMatch")
	public void startMatch(@RequestParam("userId") String userId, @RequestParam("convoId") String convoId) {
		chatSessionBusiness.startMatch(userId, convoId);
	}
	
	@RequestMapping("/stopMatch")
	public void stopMatch(@RequestParam("userId") String userId, @RequestParam("convoId") String convoId) {
		chatSessionBusiness.stopMatch(userId, convoId);
	}
	
	@RequestMapping("/removeConvo")
	public void removeConvo(@RequestParam("convoId") String convoId, 
			@RequestParam("userId") String userId, 
			@RequestParam("partnerId") String partnerId) {
		chatSessionBusiness.removeConvo(convoId, userId, partnerId);
	}
	
	@RequestMapping("/addFriend")
	public void addFriend(@RequestParam("convoId") String convoId, 
			@RequestParam("userId") String userId, @RequestParam("partnerId") String partnerId) {
		chatSessionBusiness.addFriendRequest(convoId, userId, partnerId);
	}
	
	@RequestMapping("/rejectFriend")
	public void rejectFriend(@RequestParam("convoId") String convoId, 
			@RequestParam("userId") String userId, @RequestParam("partnerId") String partnerId) {
		chatSessionBusiness.rejectFriend(convoId, userId, partnerId);
	}
	
	@RequestMapping("/acceptFriend")
	public void acceptFriend(@RequestParam("convoId") String convoId, 
			@RequestParam("userId") String userId, @RequestParam("partnerId") String partnerId) {
		chatSessionBusiness.acceptFriend(convoId, userId, partnerId);
	}
	
	@RequestMapping("/updateFriendNickName")
	public void updateFriendNickName(@RequestParam("convoId") String convoId, 
			@RequestParam("userId") String userId, @RequestParam("name") String name) {
		chatSessionBusiness.updateFriendNickName(convoId, userId, name);
	}
	
	@RequestMapping("/sendImage")
	public Message sendImage(BufferedImage bufferedImage, Message msg, 
			int left, int top, int width, int height) {
//		if (bufferedImage == null) {
//			//System.out.println("def");
//			return "";
//		}
		return chatSessionBusiness.sendImage(bufferedImage, msg, new int[]{left, top, width, height});
	}
	
	@RequestMapping("/searchAddByUsername")
	public String searchAddByUsername(@RequestParam("userId") String userId, @RequestParam("username") String username) {
		return chatSessionBusiness.searchAddByUsername(userId, username);
	}
}
